<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(1);
		$this->load->model('Candidate_site_model', 'site');
		$this->lang->load('candidate/site', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$site_list = $this->site->get(array('idcandidate' => $this->session->userdata['idprofile']));
		       	
		$grid_site = array();
        
        foreach ($site_list->result() as $row)
		{
			$url_link = $row->url;
			
			if (!parse_url($row->url, PHP_URL_SCHEME)) {
				$url_link = 'http://' . $row->url;
			}
			
			$grid_site[] = '<div class="row"><span><a class="highlightlink" href="'.$url_link.'">' . $row->url . '</a>' . (isset($row->typename) ? (' (' . $row->typename . ')') : '')
			. '<br/><span class="gdesc">'.$row->description.'</span></span><div class="gtool"><a onclick="CandidateSite.editSite('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CandidateSite.deleteSite('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_site) {
			 $data['grid_site'] = '<center>'.$this->lang->line('site_noresults').'</center>';
		} else {
			 $data['grid_site'] = implode('<hr/>', $grid_site);
		}
		
		exitHTML($data['grid_site']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('url')))
		{
			formError($this->lang->line('site_missingfield'), true);
		}
		
		if (!filter_var($data['url'], FILTER_VALIDATE_URL))
		{
			formError($this->lang->line('site_invalidlink'), true);
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		
		if ($this->site->get($data)->row())
		{
			formError($this->lang->line('site_linkexists'), true);
		}
		
		if ($this->site->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateSite.getSiteGrid();closeBox();');
		}
	}
	
	public function update($id_site = '')
	{
		$data = $_POST;
		$data['id_site'] = $id_site;
		
		if (missing_field($data, array('url')))
		{
			formError($this->lang->line('site_missingfield'), true);
		}
		
		if ($this->site->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateSite.getSiteGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('site_missingfield'));
		}
		
		if ($this->site->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CandidateSite.getSiteGrid();');
		}
	}
	
	public function edit($id_site = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_site'] = $id_site;
		
		$data['url'] = '';
		$data['description'] = '';
		
		if (is_numeric($id_site))
		{
			$phone_data = $this->site->get(array('idsite' => $id_site))->row();
			$data['url'] = $phone_data->url;
			$data['description'] = $phone_data->description;
			$data['extrascript'] = '$("#idtype").val("'.$phone_data->idtype.'");';
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('candidate/site', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
}

?>